/*
*  用来生成菜单和路由数据
*
* */
import menus from "@/data/menu"
import {RouteRecordRaw} from "vue-router"


const views = import.meta.glob('@/views/**/*.vue')
// console.log('views', views);

export const gen = (userType: number): [IMenu[], RouteRecordRaw[]] => {
    return gen2(userType, menus, '/', [], [])
}


const gen2 = (userType: number, menus: IMenu[], parentPath: string, genMenus: IMenu[], genRoutes: RouteRecordRaw[]): [IMenu[], RouteRecordRaw[]] => {

    for (let i = 0; i < menus.length; i++) {
        const menuTmp = menus[i]

        if (menuTmp.key && menuTmp.key.length > 0 && menuTmp.key.indexOf(userType) < 0) continue

        const menu = {...menuTmp}
        // console.log('----', parentPath + menu.route);
        // console.log('----', parentPath + menu.name);
        menu.route = menu.route ? parentPath + menu.route : parentPath + menu.name
        if (menu.children) {
            const [genMenusChild, genRoutesChild] = gen2(userType, menu.children, menu.route + '/', [], [])
            if (genMenusChild.length > 0) {
                menu.children = genMenusChild
                genMenus.push(menu)
                genRoutes.push({
                    path: menu.route,
                    name: menu.name,
                    redirect: genMenusChild[0].route,
                    children: genRoutesChild,
                })
            }
        } else {
            genMenus.push(menu)
            // let defaultPath = 'index'
            // console.log('menu.children no', menu.route);
            // console.log('menu.children no', views[`/src/views${menu.route}.vue`]);
            genRoutes.push({
                path: menu.route,
                name: menu.name,
                component: views[`/src/views${menu.route}.vue`],
                meta: {
                    desc: menu.desc
                },
            })
        }
    }

    return [genMenus, genRoutes]
}
